from webgraph import MikeWebgraph
import time
import psyco

if __name__ == '__main__':
    psyco.full()
    #dataset = 'th-test'
    dataset = 'wbCt100Sep08'
    time_main = time.time()
    time_overhead = time.time()
    #f = open(dataset.rank,'w')
    wg = MikeWebgraph(dataset)
    print 'time_overhead', time.time()-time_overhead

    T = 0.00001
    residual = 1
    c = 0.85

    N = int(wg.stat()['total_unique_url'])
    src_list = wg.src_list

    time_init = time.time()
    Source = [1.0/N for i in range(N)]
    Dest = [0 for i in range(N)]
    danglingnode = wg.danglingnode
    print len(danglingnode)
    print 'time_init',time.time() - time_init

    iteration = 0
    print 'start compute'
    while residual > T:
        Dest = [0 for i in range(N)]
        for src in src_list:
            dest_list = wg.forward(src)
            for dest in dest_list:
                Dest[dest] += Source[src]*1.0/len(dest_list)
        danglingScore = 0.0
        for d in danglingnode:
            danglingScore += Source[d]*1.0/N
        for i in range(N):
            Dest[i] += danglingScore
            Dest[i] = c*Dest[i] + ((1-c)/N)

        residual = float(0.0)
        for i in range(N):
            residual += abs(Source[i] - Dest[i])
        
        Source = Dest
        print iteration,residual
        iteration += 1

    #for i in range(N):
        #print i,

    print 'sum',sum(Source)
    print 'time',time.time()-time_main
